﻿Public Class GenericForm
    Dim source As String

    Enum action
        insert
        update
    End Enum

    Dim currentAction As action


    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        Me.Close()
    End Sub

    Private Sub cmdGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrabar.Click
        Dim objectSource As GenericObject
        If Me.validateMandatoryItems Then
            If currentAction = action.insert Then
                objectSource = getSourceObject(Nothing, Me.txtNombre.Text, Me.txtDescripcion.Text)
                GenericAdministrator.insert(objectSource)
            Else
                objectSource = getSourceObject(Convert.ToInt16(Me.txtId.Text), Me.txtNombre.Text, Me.txtDescripcion.Text)
                GenericAdministrator.update(objectSource)
            End If
            Me.Close()
            Dim SearchTaskTypeForm As New SearchGenericForm(source)
            SearchTaskTypeForm.Show()
        End If
    End Sub

    Private Sub TaskTypeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New(ByVal source As String, ByVal id As Integer)

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent()
        Me.source = source
        customForm()
        loadFormToEdition(id)
    End Sub

    Public Sub New(ByVal source As String)

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.source = source
        currentAction = action.insert
        customForm()
    End Sub


    Private Sub clear_form()
        Me.txtId.Text = ""
        Me.txtNombre.Text = ""
        Me.txtDescripcion.Text = ""
    End Sub


    Private Function validateMandatoryItems() As Boolean
        If Me.txtNombre.Text = Nothing Then
            MsgBox("Debe ingresar un nombre.")
            Return False
        Else
            Return True
        End If
    End Function

    Public Sub loadFormToEdition(ByVal id As Integer)
        currentAction = action.update

        Dim sourceObject As GenericObject
        sourceObject = getSourceObject()
        sourceObject.idProperty = id
        sourceObject = GenericAdministrator.searchDataById(sourceObject)
        Me.txtId.Text = id
        Me.txtNombre.Text = sourceObject.nameProperty
        Me.txtDescripcion.Text = sourceObject.descriptionProperty
    End Sub

    Private Function getSourceObject() As GenericObject
        Select Case source
            Case "ROLE"
                Return New Role()
            Case "ACTIVITY"
                Return New Activity()
            Case "TASKTYPE"
                Return New TaskType
        End Select
    End Function

    Private Sub customForm()
        Select Case source
            Case "ROLE"
                Me.Text = "Roles"
            Case "ACTIVITY"
                Me.Text = "Actividades"
            Case "TASKTYPE"
                Me.Text = "Tipos de Tareas"
        End Select
    End Sub

    Private Function getSourceObject(ByVal id As Integer, ByVal name As String, ByVal description As String) As GenericObject
        Select Case source
            Case "ROLE"
                Return New Role(id, name, description)
            Case "ACTIVITY"
                Return New Activity(id, name, description)
            Case "TASKTYPE"
                Return New TaskType(id, name, description)
        End Select
    End Function


End Class